home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Dialectic 1.1 Source / Dialectic ƒ / Dialectic code / dialectic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  4.5 KB  |  200 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        dialectic.c
  4.  
  5. Purpose:    This module handles Dialectic-specific initialization and
  6.             shutdown, memory setup for converting, and a dispatch
  7.             for file conversion.
  8.  
  9.  
  10. Dialectic -=- dialect text conversion extraordinare
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "dialectic.h"
  31. #include "dialectic dispatch.h"
  32. #include "dialectic files.h"
  33. #include "dialectic file management.h"
  34. #include "dialectic error.h"
  35. #include "dialectic progress.h"
  36. #include "program globals.h"
  37. #include "util.h"
  38. #include "msg environment.h"
  39. #include "msg menus.h"
  40.  
  41. unsigned char    gShowSaveDialog;
  42. unsigned char    gAddSuffix;
  43. unsigned char    gShowProgress;
  44. unsigned char    gUseRTF;
  45.  
  46. void InitProgram(void)
  47. {
  48.     int                i,j,k;
  49.     AppFile            myFile;
  50.     
  51.     CountAppFiles(&i, &j);
  52.     if ((j>0) && (i==0))
  53.     {
  54.         for (k=1; k<=j; k++)
  55.         {
  56.             GetAppFiles(1, &myFile);
  57.             MyMakeFSSpec(myFile.vRefNum, 0, myFile.fName, &inputFS);
  58.             StartConvert();
  59.             ClrAppFiles(k);
  60.         }
  61.     }
  62. }
  63.  
  64. void NewConvert(void)
  65. {
  66.     inputFS.name[0]=outputFS.name[0]=tempFS.name[0]=0x00;
  67.     if (GetSourceFile(&inputFS, FALSE))
  68.         StartConvert();
  69. }
  70.  
  71. void StartConvert(void)
  72. {
  73.     int                err;
  74.     Boolean            notDoneYet;
  75.     Str255            titleStr;
  76.     
  77.     outputFS=inputFS;
  78.     if (gAddSuffix)
  79.         SetupSuffix();
  80.     deleteTheThing=TRUE;
  81.     
  82.     if (gShowSaveDialog)
  83.         if (!GetDestFile(&outputFS, &deleteTheThing))
  84.             return;
  85.  
  86.     gInputNeedsUpdate=TRUE;
  87.     gOutputNeedsUpdate=FALSE;
  88.     gInputOffset=gOutputOffset=gAbsoluteOffset=0L;
  89.     
  90.     InitFiles();
  91.     HandleError(err=OpenInputFile());
  92.     if (err!=allsWell)
  93.         return;
  94.     HandleError(err=CreateTempFile());
  95.     if (err!=allsWell)
  96.     {
  97.         FinalizeFiles(FALSE);
  98.         return;
  99.     }
  100.     HandleError(err=SetupTempFile());
  101.     if (err!=allsWell)
  102.     {
  103.         FinalizeFiles(FALSE);
  104.         return;
  105.     }
  106.     
  107.     gInputBuffer=NewPtrClear(INPUT_BUFFER_MAX+16);
  108.     if (gInputBuffer==0L)
  109.     {
  110.         FinalizeFiles(FALSE);
  111.         HandleError(kNoMemory);
  112.         return;
  113.     }
  114.     
  115.     gOutputBuffer=NewPtrClear(OUTPUT_BUFFER_MAX+16);
  116.     if (gOutputBuffer==0L)
  117.     {
  118.         FinalizeFiles(FALSE);
  119.         DisposePtr(gInputBuffer);
  120.         HandleError(kNoMemory);
  121.         return;
  122.     }
  123.     
  124.     gInProgress=TRUE;
  125.     AdjustMenus();
  126.     DrawMenuBar();
  127.     
  128.     if (showThatTharProgress)
  129.     {
  130.         GetItem(gDialectMenu, gWhichDialect+1, titleStr);
  131.         OpenProgressDialog(gInputLength, titleStr);
  132.         SetProgressText("\pConverting ",inputFS.name, "\p...","\p");
  133.     }
  134.     
  135.     gInWord=gSeenI=gSeenBackslash=FALSE;
  136.     gCurlyLevel=0;
  137.     
  138.     notDoneYet=TRUE;
  139.     err=allsWell;
  140.     while ((notDoneYet) && (err==allsWell) && (gAbsoluteOffset<gInputLength))
  141.     {
  142.         if (gInputNeedsUpdate)
  143.         {
  144.             if (gInputOffset>0L)
  145.                 ShiftInputBuffer();
  146.             HandleError(err=ReadInputFile(inputRefNum, gInputBuffer+
  147.                 gWhatsReallyInInputBuffer, INPUT_BUFFER_MAX+16-gWhatsReallyInInputBuffer));
  148.             gInputNeedsUpdate=FALSE;
  149.             if (showThatTharProgress)
  150.             {
  151.                 UpdateProgressDialog(gAbsoluteOffset);
  152.                 notDoneYet=DealWithOtherPeople();
  153.             }
  154.         }
  155.         
  156.         if ((notDoneYet) && (err==allsWell) && (gOutputNeedsUpdate))
  157.         {
  158.             HandleError(err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset));
  159.             gOutputOffset=0L;
  160.             gOutputNeedsUpdate=FALSE;
  161.         }
  162.         
  163.         if ((notDoneYet) && (err==allsWell))
  164.             ConvertDispatch();
  165.     }
  166.     
  167.     if (showThatTharProgress)
  168.     {
  169.         UpdateProgressDialog(gAbsoluteOffset);
  170.         DealWithOtherPeople();
  171.     }
  172.     
  173.     if ((gOutputOffset>0L) && (err==allsWell))
  174.     {
  175.         HandleError(err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset));
  176.     }
  177.     
  178.     DisposePtr(gInputBuffer);
  179.     DisposePtr(gOutputBuffer);
  180.     
  181.     FinalizeFiles((notDoneYet) && (err==allsWell));
  182.     
  183.     if (showThatTharProgress)
  184.         DismissProgressDialog();
  185.     gInProgress=FALSE;
  186.     AdjustMenus();
  187.     DrawMenuBar();
  188. }
  189.  
  190. void ShiftInputBuffer(void)
  191. {
  192.     Mymemcpy(gInputBuffer, gInputBuffer+gInputOffset, INPUT_BUFFER_MAX+16-gInputOffset);
  193.     gWhatsReallyInInputBuffer=INPUT_BUFFER_MAX+16-gInputOffset;
  194.     gInputOffset=0L;
  195. }
  196.  
  197. void ShutDownProgram(void)
  198. {
  199. }
  200.